package com.lambda;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/***
 * @ClassName: StreamTest
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 16:45 2022/1/20
 * @version : V1.0
 */
public class StreamTest {



    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("欧阳雪",2,"中国",'F'));
        personList.add(new Person("Tom",35,"美国",'M'));
        personList.add(new Person("Harley",32,"英国",'F'));
        personList.add(new Person("向天笑",56,"中国",'M'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("小梅",54,"中国",'F'));
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康",29,"中国",'M'));

        //打印所有年龄大于20的人
        Stream<Person> personStream = personList.stream().filter((p) -> p.getAge() > 20);
        List<Person> collect = personStream.collect(Collectors.toList());
        personList.stream().filter((p) -> p.getAge() > 20).forEach(System.out::println);


        //判断是否有大于20的
        System.out.println(personList.stream().anyMatch((p) -> p.getAge() > 20));


        //判断是否所有的大于20，所有满足才满足
       System.out.println(personList.stream().allMatch((p) -> p.getAge() > 20));


       //返回第一个年龄大于20的
        System.out.println(personList.stream().filter((person -> person.getAge() > 20)).findFirst().get());


        /**
         * @Param [args]
         * @Description: TODO 按照年龄阶段分组,并且降序返回
         * @auther: TangGuoyi
         * @date: 22:54 2022/1/20
         * @return: void
         */
        Map<String, List<Person>> map1 = personList.stream()
                .collect(Collectors.groupingBy(person -> {
                    Integer age = person.getAge();
                    if ((age < 20)) {
                        return "0-20";
                    } else if ((age < 30)) {
                        return "20-30";
                    } else if ((age < 40)) {
                        return "30-40";
                    } else if ((age < 50)) {
                        return "40-50";
                    } else {
                        return "50-无穷";
                    }
                }));
        map1.forEach((v,k)->k.sort(Comparator.comparing(Person::getAge).reversed()));

        System.out.println(map1);



        /**
         * @Param [args]
         * @Description: int 数组转集合
         * @auther: TangGuoyi
         * @date: 23:44 2022/1/20
         * @return: void
         */
        int [] sss = new int[] {6,35,56,24,65,54,343,767,2,4,6,878};

        //排序
        List<Integer> list1 = Arrays.stream(sss).boxed().collect(Collectors.toList());
        list1.sort((a,b)->a.compareTo(b));
        System.out.println(list1);


        //流排序
        IntStream sorted = Arrays.stream(sss).sorted();
        List<Integer> collect1 = sorted.boxed().collect(Collectors.toList());
        System.out.println(collect1);



        //倒序
        Collections.reverse(collect1);
        System.out.println(collect1);


        //Comparator.comparing() 理解
        List<Integer> list2 = Arrays.stream(sss).boxed().collect(Collectors.toList());
        Collections.sort(list2,Comparator.comparing((a)->new Integer(a)));
        System.out.println(list2);


        List<Integer> list3 = Arrays.stream(sss).boxed().collect(Collectors.toList());

        list3.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 0;
            }
        });

        System.out.println(list3);


        /**
         * @Param
         * @Description: 转化为名字 -男女-是否大于23岁-国家
         * @auther: TangGuoyi
         * @date: 22:54 2022/1/20
         * @return:
         */
        System.out.println(personList.stream()
                //中间操作，转化映射把当前集合中的对象转换为另一个对象
                .map(person -> String.format("%s-性别是：-》%s-%s-国家是%s", person.getName()
                        , (person.getSex()=='F'?"女":"男")
                        , (person.getAge() > 23 ? "大于23" : "小于23")
                        , person.getCountry()

                )).collect(Collectors.toList()));

    }
}
